package jp.co.apl.haven_enquete.android.activity;

import jp.co.apl.haven_enquete.android.R;
import jp.dev7.android.util.Activities;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class CategoryList extends ListActivity {

    public static final int CATEGORY = 0;
    public static final int QUESTION = 1;

    private ActivityCommon activityCommon = new ActivityCommon(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        Cursor cursor =
            Activities.getFromProvider(
                this,
                "content://jp.co.apl.haven_enquete/categories");
        setListAdapter(new SimpleCursorAdapter(
            this,
            R.layout.category_list,
            cursor,
            new String[] { "title" },
            new int[] { R.id.categoryList_title }));

        activityCommon.setActionBar("カテゴリ一覧");
        Activities.findViewById(this, R.id.actionBar_nextButton).setVisibility(
            View.INVISIBLE);
    }

    @Override
    protected void onDestroy() {
        activityCommon = null;
        super.onDestroy();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(this, QuestionList.class);
        Uri uri = Uri.parse("content://jp.co.apl.haven_enquete/category/");
        uri = ContentUris.withAppendedId(uri, id);
        intent.setData(uri);

        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return activityCommon.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        return activityCommon.onOptionsItemSelected(item);
    }
}
